package com.wenguo.action.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wenguo.Constants;
import com.wenguo.bussines.model.Group;
import com.wenguo.bussines.model.ResultMessage;
import com.wenguo.bussines.model.User;
import com.wenguo.bussines.model.request.UserPassRequest;
import com.wenguo.common.util.BeansUtil;
import com.wenguo.common.util.MD5;
import com.wenguo.common.util.TimeUtil;
import com.wenguo.exception.WenGuoException;

@Controller
@RequestMapping("/user/*")
public class UserAction extends BaseAction {

	@RequestMapping("toRegister")
	public String regist(Model model) {
		model.addAttribute(Constants.system.TEMP, TimeUtil.getShortFormatLaterNyearDate(3));
		return "user/register";
	}
	@RequestMapping(value = "register", method = RequestMethod.POST)
	public @ResponseBody ResultMessage userRegister(User user){
		ResultMessage rm = new ResultMessage();
		try {
			Group group = facade.findGroupById(Integer.parseInt(Constants.group.GUEST));
			user.setGroup(group);
			user.setPass(MD5.code(user.getPass()));
			String date = TimeUtil.getFullFormatDate();
			user.setLast(date);
			user.setRegist(date);
			
			if (!facade.createUser(user)) {
				rm.setReturnCode(Constants.status.FAILED);
			}
			
		} catch (WenGuoException e) {
			logger.error(e);
		}
		
		return rm;
	}
	
	@RequestMapping("profile")
	public String profile(HttpServletRequest request, Model model){
		try {
			User user = BeansUtil.getLoginUser(request);
			User u = facade.findUserById(user.getId());
			if(null != u) {
				model.addAttribute(Constants.system.TAG_BEAN, u);
				if (u.getGroup().getId().toString().equals(Constants.group.ADMIN)) {
					List<Group> groupList = facade.findAllGroup();
					model.addAttribute(Constants.system.TAG_LIST, groupList);
				}
				return "user/profile";
			}
		} catch (WenGuoException e) {
			logger.error(e);
		}
		model.addAttribute(Constants.system.TIPS, "get user profile exception");
		return "user/profile";
	}
	
	@RequestMapping("isExist")
	public @ResponseBody ResultMessage isUserExist(@RequestParam String id){
		ResultMessage rm = new ResultMessage();
		try {
			User user = facade.findUserById(id);
			if (null != user) {
				rm.setReturnCode(Constants.status.FAILED);
			} 
		} catch (WenGuoException e) {
			e.printStackTrace();
		}
		return rm;
	}
	
	@RequestMapping("toUpdatePwd")
	public String toUserUpdatePwd(@RequestParam String id, Model model){
		
		model.addAttribute(Constants.system.ID, id);
		return "user/updatePwd";
	}
	
	@RequestMapping(value = "updatePwd", method = RequestMethod.POST)
	public @ResponseBody ResultMessage userUpdatePwd(UserPassRequest userReq){
		ResultMessage rm = new ResultMessage();
		try {
			User u = facade.findUserById(userReq.getId());
			if (u.getPass().equals(MD5.code(userReq.getOldPass()))) {
				u.setPass(MD5.code(userReq.getPass()));
				if (!facade.updateUserPassword(u)) {
					rm.setReturnCode(Constants.status.FAILED);
				}
			} else {
				rm.setReturnCode(Constants.status.FAILED);
				rm.setErrorMessage("Old password is incorrect");
			}
		} catch (WenGuoException e) {
			logger.error(e);
		}
		
		return rm;
	}
	
}
